package com.frxuexi.visit.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.VisitBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitDetailBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitDetailActivity")
@AndroidEntryPoint
class VisitDetailActivity : BaseActivity<ActivityVisitDetailBinding,VisitIntent,VisitViewModel>() {

    @Autowired
    lateinit var vid:String

    override fun getVM(): VisitViewModel {
        return viewModels<VisitViewModel>().value
    }

    override fun getBind(): ActivityVisitDetailBinding {
        return ActivityVisitDetailBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val item = it.data as VisitBeanItem
                                binding.bean=item
                                binding.visitDetailGroup.apply {
                                    if(item.visitStatus=="待审核"){
                                        visibility=View.VISIBLE
                                    }else{
                                        visibility=View.GONE
                                    }
                                }
                            }
                            RequestType.ITEM->{
                                ToastUtils.showLong("审批完成")
                                finish()
                            }
                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(VisitIntent.getVisitById(vid.toInt()))

        binding.visitDetailOk.setOnClickListener {
            vm.sendIntent(VisitIntent.approveVisit(vid.toInt()))
        }

        binding.visitDetailRefause.setOnClickListener {
            vm.sendIntent(VisitIntent.rejectVisit(vid.toInt()))
        }
    }
}